use std::{sync::mpsc,thread};
fn main(){

}

type Request = String;

// Starts a background thread performing some computation on requests sent to it.
// Delivers the response back over a oneshot channel.
fn spawn_processing_thread() -> mpsc::Sender<(Request, oneshot::Sender<usize>)> {
    let (request_sender, request_receiver) = mpsc::channel::<(Request, oneshot::Sender<usize>)>();
    thread::spawn(move || {
        for (request_data, response_sender) in request_receiver.iter() {
            let compute_operation = || request_data.len();
            let _ = response_sender.send(compute_operation()); // <- Send on the oneshot channel
        }
    });
    request_sender
}

// let processor = spawn_processing_thread();

// #[cfg(feature = "std")] {
//     let (response_sender, response_receiver) = oneshot::channel();
//     let request = Request::from("data from sync thread");

//     processor.send((request, response_sender)).expect("Processor down");
//     match response_receiver.recv_timeout(Duration::from_secs(1)) { // <- Receive on the oneshot channel
//         Ok(result) => println!("Processor returned {}", result),
//         Err(oneshot::RecvTimeoutError::Timeout) => eprintln!("Processor was too slow"),
//         Err(oneshot::RecvTimeoutError::Disconnected) => panic!("Processor exited"),
//     }
// }

// #[cfg(feature = "async")] {
//     tokio::runtime::Runtime::new()
//         .unwrap()
//         .block_on(async move {
//             let (response_sender, response_receiver) = oneshot::channel();
//             let request = Request::from("data from sync thread");

//             processor.send((request, response_sender)).expect("Processor down");
//             match response_receiver.await { // <- Receive on the oneshot channel asynchronously
//                 Ok(result) => println!("Processor returned {}", result),
//                 Err(_e) => panic!("Processor exited"),
//             }
//         });
// }